home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 5⁄4⁄90 / 0115-Re Value parameter…-Apr90 next >
Encoding:
Text File  |  1990-05-04  |  2.2 KB  |  44 lines  |  [TEXT/GEOL]

  1. Item    7403182                         28-April-90        20:12PDT
  2.  
  3. From:   MIKE.VILOT                      ObjectWare, Michael Vilot,PRT
  4.  
  5. To:     DEREK                           White, Derek
  6.  
  7. cc:     CPLUS.DEV$                      C++ Interest List--Developers
  8.         CPLUS.APPLE$                    C++ Interest List--Apple Employees
  9.  
  10. Sub:    Re: Value parameter…
  11.  
  12. Derek,
  13.     In C and C++ arguments are passed by value.  Pointers are passed by
  14. value, but they denote other objects -- hence the use of pointer arguments
  15. for side-effects in C.
  16.     C++ needs to balance a few different concerns.  The first is that it
  17. should be possible to define and use objects of user-defined classes that
  18. behave much the same as objects of the builtin types.  Thus, if I define
  19. a class complex, and create a few complex instances, it would be nice if
  20. they acted more or less like int objects or float objects.  SO, the default
  21. is to copy the object into and out of the function call (think about the
  22. implication of this for large objects).
  23.     Next, C++ supports object-oriented programming.  That means supporting
  24. polymorphism.  It does so through virtual functions.  However, there are
  25. significant differences to the ways Object Pascal and C++ handle the binding
  26. of polymorphic calls.  Because ...
  27.     Third, C++ tries to maintain the efficiency of C whenever possible.  One
  28. implication of this is that static binding of virtual calls is allowed.  That
  29. means that if the translator can detect that a given object will always have
  30. the same type, it can treat a virtual function call just like a regular one.
  31. This can be useful for certain performance-critical O-O programs.
  32.     To maintain polymorhic behavior without resorting to pointers (and their
  33. awkward syntax), you can make one simple change:  declare your argument to
  34. be a _reference_ to the base class (i.e.: void valtest(TShape& s)), and you
  35. will find the behaviour working the way you would like.
  36.     An additional  benefit is that the object as a whole will not be copied
  37. into the function, only a reference to it.  All references are the same size,
  38. independent of the size of the object it references (yes, it's implemented
  39. as a pointer, but _your_ code doesn't have to manipulate them).
  40.  
  41. Hope this helps,
  42.     Mike
  43.  
  44.